﻿using System.Net.NetworkInformation;
using Shop.Collect;
using Shop.Model.DB;
using Shop.Model.Goods;
using Shop.Model.ModuleGoods;
using Shop.RemoteModel.PageModuleGoods.Model;
using Shop.RemoteModel.SpuGoods.Model;
using Shop.Service.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Service.lmpl
{
    internal class PageModuleGoodsService : IPageModuleGoodsService
    {
        private readonly IPageModuleGoodsCollect _Service;
        private readonly IGoodsCollect _Goods;
        private readonly ICategoryCollect _Category;
        public PageModuleGoodsService ( IPageModuleGoodsCollect service,
            ICategoryCollect category,
            IGoodsCollect goods )
        {
            this._Category = category;
            this._Service = service;
            this._Goods = goods;
        }

        public void Delete ( long id )
        {
            DBPageModuleGoods dto = this._Service.Get(id);
            this._Service.Delete(dto);
        }
        public void Delete ( long[] ids )
        {
            this._Service.Delete(ids);
        }
        public Dictionary<long, int> SetSort ( long id, int sort )
        {
            DBPageModuleGoods dto = this._Service.Get(id);
            return this._Service.SetSort(dto, sort);
        }
        public ModuleGoodsDto[] GetGoods ( long tModuleId, string tag )
        {
            ModuleGoodsBase[] goods = this._Service.GetGoods(tModuleId, tag);
            if ( goods.IsNull() )
            {
                return Array.Empty<ModuleGoodsDto>();
            }
            return this._Format(goods);
        }

        public ModuleGoodsDto[] Sync ( ModuleGoodsAdd add )
        {
            ModuleGoodsBase[] goods = this._Service.Sync(add);
            if ( goods.IsNull() )
            {
                return Array.Empty<ModuleGoodsDto>();
            }
            return this._Format(goods);
        }
        public SpuDatum[] GetTops ( long moduleId, string tag, int top )
        {
            long[] goodsId = _Service.GetGoodsId(moduleId, tag, top);
            if ( goodsId.IsNull() )
            {
                return Array.Empty<SpuDatum>();
            }
            return _Goods.Gets<SpuDatum>(goodsId);
        }
        public PagingResult<SpuDatum> Query ( long moduleId, string tag, IBasicPage paging )
        {
            long[] spuId = _Service.Query(moduleId, tag, paging, out int count);
            if ( spuId.IsNull() )
            {
                return new PagingResult<SpuDatum>(count);
            }
            SpuDatum[] spus = _Goods.Gets<SpuDatum>(spuId);
            return new PagingResult<SpuDatum>(spus, count);
        }

        private ModuleGoodsDto[] _Format ( ModuleGoodsBase[] goods )
        {
            long[] goodsId = goods.ConvertAll(c => c.GoodsId);
            GoodsBaseDto[] dtos = this._Goods.Gets<GoodsBaseDto>(goodsId);
            Dictionary<long, string> names = this._Category.GetFullNames(dtos.Distinct(c => c.CategoryId));
            ModuleGoodsDto[] list = dtos.ConvertMap<GoodsBaseDto, ModuleGoodsDto>();
            list.ForEach(a =>
            {
                ModuleGoodsBase state = goods.Find(c => c.GoodsId == a.Id);
                a.Id = state.Id;
                a.GoodsId = state.GoodsId;
                a.Sort = state.Sort;
                a.Category = names.GetValueOrDefault(a.CategoryId);
            });
            return list;
        }
    }
}
